const pkg = require('./package.json')
const path = require('path')
const webpack = require('webpack')

module.exports = {
  pages: {
    index: {
      entry: 'src/main.ts',
      title: `${process.env.VUE_APP_TITLE_PREFIX}${pkg.title}`,
      template: 'public/index.html',
      chunks: 'all',
    },
  },
  publicPath: process.env.VUE_APP_ASSET,
  outputDir: 'dist',
  productionSourceMap: false,
  devServer: {
    historyApiFallback: false,
    noInfo: false,
    proxy: {
      '/up': {
        target: `http://127.0.0.1:8888`,
        changeOrigin: true,
      },
    },
    open: true,
    host: '0.0.0.0',
    port: 9527,
    openPage: 'index.html',
  },
  css: {
    loaderOptions: {
      scss: {
        prependData: `@import "@/styles/fn.scss";`,
      },
    },
  },
  configureWebpack: (config) => {
    const SafariNomoduleFixPlugin = config.plugins.find(
      (item) => item?.constructor?.name === 'SafariNomoduleFixPlugin',
    )

    if (SafariNomoduleFixPlugin) {
      SafariNomoduleFixPlugin.unsafeInline = true
    }
    config.resolve.alias = {
      '@': path.resolve(__dirname, './src'),
    }

    config.optimization.splitChunks = {
      chunks: 'all',
      minChunks: 2,
      minSize: 50 * 1024 * 8,
      cacheGroups: {
        vue: {
          name: 'vue-lib',
          test: /[\\/]node_modules[\\/](vue|vue-router|vuex|vue-class-component|vue-property-decorator|@babel[\\/]runtime|vue-lazyload)[\\/]/,
          enforce: true,
        },
        vconsole: {
          name: 'vconsole',
          test: /[\\/]node_modules[\\/](vconsole)[\\/]/,
          chunks: 'async',
          enforce: true,
        },
        lib: {
          name: 'third-lib',
          test: /[\\/]node_modules[\\/](alloyfinger|axios|core-js|crypto-js|dayjs|js-base64|register-service-worker|url-parse|uuid|@tp[\\/]bridge|@tp[\\/]beidou-js-sdk)[\\/]/,
          enforce: true,
        },
        ui: {
          name: 'ui-lib',
          test: /[\\/]node_modules[\\/](vant|animate.css|)[\\/]/,
          enforce: true,
        },
        html2canvas: {
          name: 'canvas',
          test: /[\\/]node_modules[\\/](html2canvas|canvas|clipboard)[\\/]/,
          chunks: 'async',
          enforce: true,
        },
        editor: {
          name: 'editor',
          test(module) {
            // `module.resource` contains the absolute path of the file on disk.
            // Note the usage of `path.sep` instead of / or \, for cross-platform compatibility.
            return (
              module.resource &&
              (module.resource.includes(`${path.sep}configure${path.sep}`) ||
                module.resource.includes(`${path.sep}editor${path.sep}`))
            )
          },
          chunks: 'async',
          enforce: true,
        },
        plugins: {
          name: 'plugins',
          test(module) {
            return (
              module.resource &&
              (module.resource.includes(
                `${path.sep}src${path.sep}plugins${path.sep}`,
              ) ||
                module.resource.includes(
                  `${path.sep}common${path.sep}components${path.sep}`,
                ) ||
                module.resource.includes(
                  `${path.sep}common${path.sep}constant${path.sep}`,
                ) ||
                module.resource.includes(
                  `${path.sep}common${path.sep}mixins${path.sep}`,
                ) ||
                module.resource.includes(
                  `${path.sep}common${path.sep}utils${path.sep}`,
                ))
            )
          },
          // chunks: 'async',
          enforce: true,
        },
        api: {
          name: 'api',
          test(module) {
            return (
              module.resource &&
              module.resource.includes(`${path.sep}api${path.sep}`)
            )
          },
          enforce: true,
        },
      },
    }
    config.plugins.push(
      new webpack.optimize.MinChunkSizePlugin({
        minChunkSize: 30000,
      }),
    )
  },
  chainWebpack: (config) => {
    config.optimization.minimizer('terser').tap((args) => {
      args[0].parallel = 4
      args[0].terserOptions.compress.warnings = false
      args[0].terserOptions.compress.drop_console = true
      args[0].terserOptions.compress.drop_debugger = true

      return [...args]
    })
  },
}
